package com.fengye.school.service;

import com.fengye.school.model.school.Subject;
import com.fengye.school.response.ResponsePageResult;
import com.fengye.school.respository.SubjectRepository;
import com.fengye.school.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SubjectService {

    @Resource
    private SubjectRepository subjectRepository;

    public Subject save(Subject subject){
        Subject save = subjectRepository.save(subject);
        log.info("保存成功, subject={}", save);
        return save;
    }

    public List<Subject> list() {
        return subjectRepository.findAll();
    }

    public PageVO<Subject> listForPage(int page, int size) {
        return null;
    }

    public ResponsePageResult<Subject> listByPage(int max, int limit) {
        Pageable pageable = PageRequest.of(max, limit, Sort.Direction.ASC, "updateTime");
        Page<Subject> all = subjectRepository.findAll(pageable);
        return ResponsePageResult.success("查询专业成功", all.getTotalElements(), all.getContent());
    }

    public void deleteAll(Long[] ids) {
        Arrays.stream(ids).forEach(id -> subjectRepository.deleteById(id));
    }

    public void deleteById(Long id) {
        subjectRepository.deleteById(id);
    }
}
